<template>
	<view>
		<navbar title="我要载入" :autoBack="true"></navbar>
		<scroll-view scroll-y="true" class="scbox">
			<view class="con">
				<view class="con__box">
					<view class="con__box__item">
						<view class="con__box__item__label">
							标题
						</view>
						<input type="text" class="con__box__item__inp" v-model="form.title" placeholder="请输入标题" />
					</view>
					<view class="con__box__item">
						<view class="con__box__item__label">
							姓名
						</view>
						<input type="text" class="con__box__item__inp" v-model="form.name" placeholder="请输入姓名" />
					</view>
					<view class="con__box__item" @click="showtime=true">
						<view class="con__box__item__label">
							生日
						</view>
						<view class="con__box__item__inp" style="color: grey;" v-if="!form.birthday">
							请选择生日
						</view>
						<view class="con__box__item__inp" v-else>
							{{form.birthday}}
						</view>
					</view>
					<view class="con__box__item">
						<view class="con__box__item__label">
							性别
						</view>
						<view class="u-flex u-flex-y-center con__box__item__inp u-flex-end">
							<view class="u-flex u-flex-y-center" @click="form.sex='女'">
								<view class="con__box__item__checkbox">
									<image src="/static/my/check.png" class="con__box__item__checkbox__icon"
										v-if="form.sex=='女'" mode=""></image>
									<image src="/static/my/nocheck.png" class="con__box__item__checkbox__icon" v-else
										mode=""></image>
								</view>
								女
							</view>
							<view class="u-flex u-flex-y-center" @click="form.sex='男'">
								<view class="con__box__item__checkbox">
									<image src="/static/my/check.png" class="con__box__item__checkbox__icon"
										v-if="form.sex=='男'" mode=""></image>
									<image src="/static/my/nocheck.png" class="con__box__item__checkbox__icon" v-else
										mode=""></image>
								</view>
								男
							</view>
						</view>
					</view>
					<view class="" style="margin-top: 20rpx;">
						<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
							:maxCount="1"></u-upload>
					</view>
					<u--textarea v-model="form.intro" placeholder="请输入简介..."
						placeholderStyle='color:#919191;font-size:24rpx;' count maxlength='500'
						border='bottom'></u--textarea>
					<u--textarea v-model="form.content" placeholder="想载入的理由..."
						placeholderStyle='color:#919191;font-size:24rpx;' count maxlength='500'
						border='bottom'></u--textarea>
					<view class="footer" @click="submit">
						<image src="/static/my/btn.png" class="btn" mode=""></image>
						<view class="footer__text">
							确认提交
						</view>
					</view>
				</view>
			</view>
		</scroll-view>

		<u-datetime-picker :show="showtime" v-model="value1" mode="date" @close='show=false' @cancel='showtime=false' @confirm='confirm' :minDate='0'></u-datetime-picker>
	</view>
</template>

<script>
	import home from '@/api/_home.js'
	export default {
		data() {
			return {
				showtime: false,
				form: {
					name: '',
					birthday: '',
					sex: '',
					intro: '',
					content: '',
					image: '',
					title: ''
				},
				fileList1: [],
				value1: Number(new Date()),
			};
		},
		methods: {
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {

					const result = await this.uploadFilePromise(lists[i].url)

					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, result))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'http://88.fuliaoxx.com/api/Common/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						header: {
							token: uni.getStorageSync('88ftoken')
						},
						formData: {
							user: 'test'
						},
						success: (res) => {
							setTimeout(() => {
								// console.log(JSON.parse(res.data))
								if (JSON.parse(res.data).code == 401) {
									uni.showToast({
										title: JSON.parse(res.data).msg,
										icon: 'none'
									})
									setTimeout(() => {
										uni.reLaunch({
											url: '/pages/index/login'
										})
									}, 1000)
								}
								if (JSON.parse(res.data).code == 0) {
									uni.showToast({
										title: JSON.parse(res.data).msg,
										icon: 'none'
									})
									setTimeout(() => {
										resolve({
											status: 'error',
											message: '',
											url: ''
										})
									}, 1000)

								}
								if (JSON.parse(res.data).code == 1) {
									resolve({
										status: 'success',
										message: '',
										url: JSON.parse(res.data).data.fullurl,
										fullurl: JSON.parse(res.data).data.url
									})
								}
							}, 1000)
						}
					});
				})
			},
			confirm(e){
				// console.log(e)
				this.form.birthday = uni.$u.timeFormat(e.value, 'yyyy-mm-dd');
				this.showtime=false
			},
			submit() {
				if (this.fileList1.length > 0) {
					this.form.image = this.fileList1[0].fullurl
				}
				let {
					title,
					name,
					image,
					intro,
					birthday,
					sex,
					content
				} = this.form
				if (!title) {
					uni.$u.toast('请输入标题')
					return
				}
				if (!name) {
					uni.$u.toast('请输入姓名')
					return
				}
				if (!birthday) {
					uni.$u.toast('请填写生日')
					return
				}
				if (!sex) {
					uni.$u.toast('请选择性别')
					return
				}
				if (!image) {
					uni.$u.toast('请上传图片')
					return
				}
				if (!intro) {
					uni.$u.toast('请输入简介')
					return
				}
				if (!content) {
					uni.$u.toast('请输入理由')
					return
				}
				home.createGlobal(this.form).then(res => {
					if (res.data.code == 1) {
						uni.$u.toast('上传成功，等待审核')
						setTimeout(() => {
							uni.navigateBack()
						}, 1000)
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #F0F3F7;
	}

	.con {
		padding: 20rpx 30rpx;

		&__box {
			padding: 16rpx 0 85rpx 30rpx;
			background-color: #fff;
			border-radius: 30rpx;

			&__item {
				display: flex;
				align-items: center;
				border-bottom: 2rpx solid #F9F9F9;
				height: 106rpx;
				padding-right: 35rpx;

				&__label {
					font-family: PingFang SC;
					font-weight: 500;
					font-size: 26rpx;
					color: #6D6D6D;
					line-height: 26rpx;
				}

				&__inp {
					text-align: right;
					font-family: PingFang SC;
					font-weight: 500;
					font-size: 26rpx;
					color: #030303;
					line-height: 38rpx;
					flex: 1;
				}

				&__checkbox {
					display: flex;
					align-items: center;
					justify-content: center;
					margin-right: 6rpx;
					margin-left: 70rpx;

					&__icon {
						width: 35rpx;
						height: 35rpx;
					}
				}
			}
		}
	}

	::v-deep.u-textarea {
		padding: 37rpx 0;

		.u-textarea__count {
			font-family: PingFang SC;
			font-weight: 500;
			font-size: 24rpx;
			color: #AFAFAF;
			line-height: 24rpx;
			padding-bottom: 28rpx;
		}
	}

	::v-deep.u-border-bottom {
		border-bottom-width: 2rpx !important;
		border-color: #F9F9F9 !important;
	}

	.footer {
		position: relative;
		margin-top: 100rpx;

		.btn {
			width: 615rpx;
			height: 105rpx;
		}

		&__text {
			width: 615rpx;
			display: flex;
			justify-content: center;
			font-family: PingFang SC;
			font-weight: 500;
			font-size: 32rpx;
			color: #3A4E6D;
			line-height: 32rpx;
			// padding-top: 33rpx;
			position: absolute;
			padding-top: 33rpx;
			top: 0;
			left: 0;
		}
	}
</style>